package HomeWork;
/*
 *@auther will
 *@version 1.0
 */

public class Homework06 {
    public static void main(String[] args) {
        Person tang = new Person("唐僧", new Boat());
        tang.common();//一般情况下
        tang.passRiver();//过河
        tang.common();//一般情况下
        tang.passRiver();//过河
        tang.common();//一般情况下
        tang.passRiver();//过河
        tang.passFier();
    }
}
class Person{
    private String name;
    private Vehicles vehicles;

    public Person() {
    }

    //在创建人对象时，实现给他分配一个交通工具
    public Person(String name, Vehicles vehicles) {
        this.name = name;
        this.vehicles = vehicles;
    }
    //实例化Person对象"唐僧",要求一般情况下用Horse作为交通工具，遇到大河时用Boat作为交通工具
    //把具体的要求封装成方法
    public void passRiver(){
        //vehicles instanceof Boat判断当前的vehicles是不是 Boat
        //1. vehicles为null : vehicles instanceof Boat==> false
        //2. vehicles == 马对象 vehicles instanceof Boat==> false
        if (!(vehicles instanceof Boat)){
            vehicles = Factory.getBoat();
        }
            vehicles.work();
    }

    public void common(){
        if (!(vehicles instanceof Horse)){
            vehicles = Factory.getHorse();
        }
        vehicles.work();
    }

    //过火焰山的情况
    public void passFier(){
        if (!(vehicles instanceof Plane)){
            vehicles = Factory.getPlane();
        }
        vehicles.work();
    }
}


interface Vehicles{
    void work();
}

class Horse implements Vehicles{

    @Override
    public void work() {
        System.out.println("一般情况下使用马儿前进...");
    }
}

class Boat implements Vehicles{
    @Override
    public void work() {
        System.out.println("过河的时候使用小船...");
    }
}

class Factory{
    //创建交通工具工厂类，有两个方法分别获得交通工具 Horse和Boat
    //马儿始终是同一匹
    private static Horse horse = new Horse();

    private Factory(){}
    public static Horse getHorse(){
        return horse;
    }

    //每次不是同一艘船
    public static Boat getBoat(){
        return new Boat();
    }

    public static Plane getPlane(){
        return new Plane();
    }
}
class Plane implements Vehicles{
    @Override
    public void work() {
        System.out.println("过火焰山坐飞机");
    }
}